package fun.yizhierha.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BadRequestException;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.utils.PageUtils;
import fun.yizhierha.common.utils.Query;
import fun.yizhierha.common.utils.SecurityUtils;
import fun.yizhierha.common.utils.ValidList;
import fun.yizhierha.common.utils.file.ExcelUtils;
import fun.yizhierha.operation.domain.OraDeployHistory;
import fun.yizhierha.operation.domain.vo.CreateOraDeployHistoryVo;
import fun.yizhierha.operation.domain.vo.UpdateOraDeployHistoryVo;
import fun.yizhierha.operation.domain.vo.RetrieveOraDeployHistoryVo;
import fun.yizhierha.operation.mapper.OraDeployHistoryMapper;
import fun.yizhierha.operation.service.mapstruct.OraDeployHistoryMapstruct;
import fun.yizhierha.operation.service.OraDeployHistoryService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/** generated by EH-Admin
* @author xaopohi
* @date Wed Dec 21 15:55:14 CST 2022
**/
@Service
@RequiredArgsConstructor
public class OraDeployHistoryServiceImpl extends ServiceImpl<OraDeployHistoryMapper, OraDeployHistory> implements OraDeployHistoryService{

    private final OraDeployHistoryMapstruct oraDeployHistoryMapstruct;

    @Override
    public PageUtils<OraDeployHistory> list(RetrieveOraDeployHistoryVo retrieveOraDeployHistoryVo, Query.PageVo pageVo) {
        QueryWrapper<OraDeployHistory> wrapper = new QueryWrapper<>();
        Long deployId = retrieveOraDeployHistoryVo.getDeployId();
        String appName = retrieveOraDeployHistoryVo.getAppName();
        if (deployId != null) wrapper.eq(OraDeployHistory.COL_DEPLOY_ID,deployId);
        if (appName != null) wrapper.like(OraDeployHistory.COL_APP_NAME,appName);
        IPage<OraDeployHistory> iPage = baseMapper.selectPage(new Query<OraDeployHistory>().getPage(pageVo), wrapper);
        return new PageUtils<>(iPage);
    }

    @Override
    public synchronized void save(CreateOraDeployHistoryVo createOraDeployHistoryVo) {
        // 1.字段为UNI，需要不重复
//        UserDetails currentUser = SecurityUtils.getCurrentUser();
//        createOraDeployHistoryVo.setCreateBy(currentUser.getUsername());
        // 2.映射数据
        OraDeployHistory oraDeployHistory = oraDeployHistoryMapstruct.toOraDeployHistory(createOraDeployHistoryVo);
        // 3.保存    
        this.save(oraDeployHistory);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void edit(ValidList<UpdateOraDeployHistoryVo> updateOraDeployHistoryVoList, List<BaseErrDto> errDtoList) {
        List<OraDeployHistory> toUpdateOraDeployHistoryList = new ArrayList<>();

        for (UpdateOraDeployHistoryVo updateOraDeployHistoryVo : updateOraDeployHistoryVoList) {
            Long id = updateOraDeployHistoryVo.getId();
            // 1.字段为UNI，需要不重复

            OraDeployHistory oraDeployHistory = oraDeployHistoryMapstruct.toOraDeployHistory(updateOraDeployHistoryVo);

            toUpdateOraDeployHistoryList.add(oraDeployHistory);
        }

        // 2.更新
        this.updateBatchById(toUpdateOraDeployHistoryList);
    }

    @Override
    public void remove(Set<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void download(HttpServletResponse response) {
        ExcelUtils.export(response,"部署管理信息表",this.list(), OraDeployHistory.class);
    }

}